﻿using Newtonsoft.Json;
using PIMS.DBModel;
using PIMS.Interface;
using PIMS.WorkPlugin.ORM;
using PIMS.WorkPlugin.WMS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PIMS.WorkPlugin
{
    public partial class FormManualWMS : Form
    {
        private IDebugMsg debug = null;
        private ILog4net log = null;
        private int lineNO = 0;

        private string _httpUrl = "http://172.18.66.6:9080/api/ltsh/Pack/SendPalletInfo";

        public FormManualWMS()
        {
            InitializeComponent();
        }

        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;

            //try
            //{
            //    InitComboBoxByString(this.cb包装设备, pimsConfig.GetAppValueByKey("deviceName"));
            //    DataDictTools.InitComboBoxByDataDic(this.cb品名, "品名");
            //    DataDictTools.InitComboBoxByDataDic(this.cb牌号, "牌号");
            //    DataDictTools.InitComboBoxByDataDic(this.cb等级, "等级");
            //}
            //catch (Exception ex)
            //{
            //    this.debug.DebugMsg(ex.Message, InfoLevel.error);
            //    this.log.Error("", ex);
            //}
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.err.Clear();

            if (String.IsNullOrEmpty(this.cbxLineNO.Text))
            {
                this.err.SetError(this.cbxLineNO, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtWorkNum.Text))
            {
                this.err.SetError(this.txtWorkNum, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtProject.Text))
            {
                this.err.SetError(this.txtProject, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtFactory.Text))
            {
                this.err.SetError(this.txtFactory, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtItemCode.Text))
            {
                this.err.SetError(this.txtItemCode, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtUnit.Text))
            {
                this.err.SetError(this.txtUnit, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtProductLine.Text))
            {
                this.err.SetError(this.txtProductLine, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtBatch.Text))
            {
                this.err.SetError(this.txtBatch, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtSmallNO.Text))
            {
                this.err.SetError(this.txtSmallNO, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtCount.Text))
            {
                this.err.SetError(this.txtCount, "禁止为空！");
                return;
            }
            if (!IsInt(this.txtCount.Text))
            {
                this.err.SetError(this.txtCount, "请输入正整数！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtLongNO.Text))
            {
                this.err.SetError(this.txtLongNO, "禁止为空！");
                return;
            }

            string QulityInfo = string.IsNullOrEmpty(this.txtQulityInfo.Text) ? " " : this.txtQulityInfo.Text;
            string PecoalNote = string.IsNullOrEmpty(this.txtPecoalNote.Text) ? " " : this.txtPecoalNote.Text;
            string ProductLevel = string.IsNullOrEmpty(this.txtProductLevel.Text) ? " " : this.txtProductLevel.Text;
            string ProductStage = string.IsNullOrEmpty(this.txtProductStage.Text) ? " " : this.txtProductStage.Text;
            string txtReclaim = string.IsNullOrEmpty(this.txtReclaimLogo.Text) ? " " : this.txtReclaimLogo.Text;

            try
            {
                saveddata save = new saveddata();

                Msg_WMS wmsInfo = new Msg_WMS();

                save.line = lineNO;
                save.workOrder = this.txtWorkNum.Text;
                save.reserved12 = this.txtFactory.Text;
                save.reserved1 = this.txtProject.Text;
                save.reserved2 = this.txtItemCode.Text;
                save.grade = this.txtUnit.Text;
                save.reserved9 = this.txtQulityInfo.Text;
                save.reserved11 = this.txtPecoalNote.Text;
                save.reserved10 = this.txtProductLevel.Text;
                save.reserved8 = this.txtProductStage.Text;
                save.reserved6 = this.txtReclaimLogo.Text;
                save.packager = this.txtProductLine.Text;
                save.reserved5 = this.txtSmallNO.Text;
                save.batchNo = this.txtBatch.Text;
                save.uid = this.txtLongNO.Text;
                save.count = this.txtCount.Text;
                save.productTime = DateTime.Now;

                //wmsInfo.ZZDHI = save.reserved1;
                //wmsInfo.WERKS = save.reserved12;
                //wmsInfo.LGORT = null;
                //wmsInfo.MATNR = save.reserved2;
                //wmsInfo.MENGE = int.Parse(save.count);
                //wmsInfo.MEINS = save.grade;
                //wmsInfo.LPN = save.uid;
                //wmsInfo.CHARG = null;
                //wmsInfo.HSBA001 = DateTime.Now;
                //wmsInfo.HSBA002 = DateTime.Now;
                //wmsInfo.HSBA003 = save.reserved9;
                //wmsInfo.HSBA004 = save.reserved11;
                //wmsInfo.HSBA005 = save.reserved10;
                //wmsInfo.HSBA006 = save.reserved8;
                //wmsInfo.HSBA007 = save.reserved6;
                //wmsInfo.HSBA010 = save.packager;
                //wmsInfo.HSBA011 = save.reserved5;
                //wmsInfo.HSBA012 = save.batchNo;
                //wmsInfo.ZTYPE = "A002";
                //wmsInfo.ZZDHH = save.workOrder;
                //wmsInfo.details = listInfoItem;

                string msg = JsonConvert.SerializeObject(wmsInfo);
                var mesResult = WMSHttp.HttpPost(_httpUrl, msg);
                Msg_WMS_Return msg_WMS_Return = (Msg_WMS_Return)JsonConvert.DeserializeObject<Msg_WMS_Return>(mesResult);
                if (msg_WMS_Return.ErrCode == "0")
                {
                    this.debug.DebugMsg($"{lineNO}号包装线手动入库成功!生产订单号{save.workOrder},唯一码{save.uid}");
                    this.log.Info($"{lineNO}号包装线手动入库成功!生产订单号{save.workOrder},唯一码{save.uid}");
                    SqlSugarHelper.InsertRecord<saveddata>(save);
                    this.debug.DebugMsg($"{lineNO}号包装线添加生产记录,生产订单号{save.workOrder},唯一码{save.uid},包数{save.count}");
                    this.log.Info($"{lineNO}号包装线添加生产记录,生产订单号{save.workOrder},唯一码{save.uid},包数{save.count}");
                }
                else
                {
                    this.debug.DebugMsg($"{lineNO}号包装线手动入库失败！返回信息：{msg_WMS_Return.ErrMess}  接口报文：{msg}", InfoLevel.error);
                    this.log.Error($"{lineNO}号包装线手动入库失败！返回信息：{msg_WMS_Return.ErrMess}  接口报文：{msg}");
                }

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.debug.DebugMsg(ex.Message, InfoLevel.error);
                this.log.Error("", ex);
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbxLineNO_SelectedIndexChanged(object sender, EventArgs e)
        {
            lineNO=int.Parse(this.cbxLineNO.Text);
            orderformcurrent current = SqlSugarHelper.QueryByKey<orderformcurrent>(lineNO);
            if (!string.IsNullOrEmpty(current.workOrder))
            {
                this.txtWorkNum.Text = current.workOrder;
                this.txtProject.Text = current.reserved1;
                this.txtFactory.Text = current.reserved12;
                this.txtItemCode.Text = current.reserved2;
                this.txtUnit.Text = current.grade;
                this.txtQulityInfo.Text = current.reserved9;
                this.txtPecoalNote.Text = current.reserved11;
                this.txtProductLevel.Text = current.reserved10;
                this.txtProductStage.Text = current.reserved8;
                this.txtReclaimLogo.Text = current.reserved6;
                this.txtProductLine.Text = current.packager;
            }
            else
            {
                this.txtWorkNum.Text = string.Empty;
                this.txtProject.Text = string.Empty;
                this.txtFactory.Text = string.Empty;
                this.txtItemCode.Text = string.Empty;
                this.txtUnit.Text = string.Empty;
                this.txtQulityInfo.Text = string.Empty;
                this.txtPecoalNote.Text = string.Empty;
                this.txtProductLevel.Text = string.Empty;
                this.txtProductStage.Text = string.Empty;
                this.txtReclaimLogo.Text = string.Empty;
                this.txtProductLine.Text = string.Empty;
            }
        }

        //判断是否为正整数
        public static bool IsInt(string inString)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(inString.Trim());
        }           
    }
}
